package core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class A2 {

	private static final String BUILD_PROPERTIES_FILE = "build.properties";

	private static final String PATH_ROOT = "path.root";
	private static final String PATH_OUTPUT = "path.output";

	private static final String PROJECT_NAME = "project.name";
	private static final String PROJECT_VERSION = "project.version";

	private static final String PATH_CUT = "path.cut";
	
	public static void readRecursive(File f, String pathCut, StringBuilder sb,
			StringBuilder log, StringBuilder clutter) throws IOException {
		if (f.isDirectory()) {
			String[] list = f.list();
			for (int i = 0; i < list.length; i++) {
				File f2 = new File(f.getAbsoluteFile() + "\\" + list[i]);
				readRecursive(f2, pathCut, sb, log, clutter);
			}
		} else if (f.getName().endsWith(".jplugin")) {
			// append to manifest
			
			if (pathCut != null){
				sb.append("\n//! import \"" + f.getCanonicalPath().replace(pathCut, "") + "\"");
				log.append("\nimporting file (cutting path) " + f.getCanonicalPath());
			} else {
				log.append("\nimporting file " + f.getCanonicalPath());
				sb.append("\n//! import \"" + f.getCanonicalPath()+ "\"");	
			}
			
		} else if (f.getName().endsWith(".j")) {
			/*
			 * FileReader r = new FileReader(f); clutter.append("\n\n");
			 * clutter.append("// contents from " + f.getName() + "\n\n"); char
			 * [] line = new char[1024 *20];
			 * 
			 * while ( r.read(line) != -1){ clutter.append(line); }
			 * clutter.append("\n\n"); r.close();
			 */
		}
	}

	public static void main(String[] args) throws IOException {
		StringBuilder log = new StringBuilder();
		StringBuilder clutter = new StringBuilder();
		StringBuilder sb = new StringBuilder();

		Properties p = new Properties();
		log.append("\nloading properties");
		p.load(new FileInputStream(BUILD_PROPERTIES_FILE));
		String path = p.getProperty(PATH_ROOT);
		String outPath = p.getProperty(PATH_OUTPUT);
		String projectName = p.getProperty(PROJECT_NAME);
		String projectVersion = p.getProperty(PROJECT_VERSION);
		String pathCut = p.getProperty(PATH_CUT);
		
		String projectString = projectName + "_" + projectVersion;
		
		FileWriter outW = null;
		try {
			log.append("\nretrieving file path");
			File f = new File(path);
			f.getCanonicalPath();

			log.append("\nreading files (cutting " + pathCut + ") ");

			sb.append("// Import for project " + projectString + ":");
			readRecursive(f, pathCut, sb, log, clutter);
			log.append("\nconfiguring output path ");

			File out = new File(outPath + "\\" +projectString + ".manifest.j");
			log.append("\nwriting output file " + out.getCanonicalPath());
			outW = new FileWriter(out);
			outW.append(sb.toString());

		} catch (Exception e) {
			log.append("\n" + e.getMessage());
		} finally {
			if (outW != null) {
				outW.flush();
				outW.close();
			}
		}

		log.append("\nwriting log file ");
		File out = new File(outPath + "\\build.log.txt");
		outW = new FileWriter(out);
		outW.append(log.toString());
		outW.flush();
		outW.close();
	}
}
